from openai import OpenAI
from openai.types.chat import ChatCompletion

BASE_URL: str = "https://api.chatfire.cn/v1"
API_KEY: str = "sk-zO8exlBicZh7nJeZn5GuC5X9SPuVrZzXoGyOW0i9BFvN62ON"
client: OpenAI = OpenAI(base_url=BASE_URL, api_key=API_KEY)

def req_step_1():
    completion: ChatCompletion = client.chat.completions.create(
    model="gpt-4o",
    messages=[{
          "role": "developer",
          "content": "你是需求建模专家，需按以下步骤处理软件需求：1. 提取核心实体（如‘订单’‘用户’）；2. 梳理关键动作（如‘创建订单’‘校验库存’）；\
          3. 列出约束条件（如‘库存扣减失败则订单失败’）；4. 用JSON格式输出结构化需求模型。"
        },
        {
          "role": "user",
          "content": "需求：用户点击‘立即购买’创建订单，需校验库存，成功返回订单号，记录日志。"
        }],
    )
    print(completion.choices[0].message.content)

#req_step_1()

def req_step_2():
    completion: ChatCompletion = client.chat.completions.create(
    model="gpt-4o",
    messages=[
    {
      "role": "developer",
      "content": "根据结构化需求，生成 UML 用例图描述，包含：\n- 参与者（用户、系统）；\n- 用例（创建订单、校验库存、校验地址等）；\n- 用例之间的关系（包含、扩展）。\n用 PlantUML 语法输出，方便后续转可视化图。"
    },
    {
      "role": "user",
      "content": "{\"entities\":{\"User\":{},\"Order\":{\"attributes\":[\"orderId\"]},\"Inventory\":{},\"Log\":{}},\"actions\":[{\"action\":\"clickBuyNow\",\"actor\":\"User\",\"description\":\"User clicks '立即购买' to create an order.\"},{\"action\":\"createOrder\",\"actor\":\"System\",\"description\":\"System attempts to create an order.\"},{\"action\":\"checkInventory\",\"actor\":\"System\",\"description\":\"System checks the inventory levels.\"},{\"action\":\"returnOrderId\",\"actor\":\"System\",\"description\":\"System returns the order ID if inventory check is successful.\"},{\"action\":\"recordLog\",\"actor\":\"System\",\"description\":\"System records a log entry for the action.\"}],\"constraints\":[{\"if\":\"checkInventory is successful\",\"then\":\"createOrder and returnOrderId\"},{\"if\":\"checkInventory fails\",\"then\":\"recordLog indicating order creation failure\"}]}"
    }],
    )
    print(completion.choices[0].message.content)

req_step_2()
